//
//  HotAskViewController.m
//  SHHCPhone
//
//  Created by Sheng long on 14-9-21.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "HotAskViewController.h"
#import "HotAskView.h"
#import "ConsultViewController.h"
#import "HomeSideViewController.h"

@interface HotAskViewController ()<AutoRefreshTableViewDelegate>
{
    //最热问题页面
    HotAskView *_askView;
    
    //初次
    BOOL isFirst;
    
    //button背景
    UIButton *bgButton;
    
    //是否在搜索状态
    BOOL _isSearchState;
}

@property (nonatomic,retain) NSArray *normalArray;

@end

@implementation HotAskViewController

@synthesize normalArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:GET_COLOR(@"#f7f7f7")];
    
    _askView = [[HotAskView alloc] initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, CURRENT_CONTENT_WIDTH, CURRENT_CONTENT_HEIGHT-NAVIGATIONBAR_HEIGHT)];
    [_askView setAskVc:self];
    [self.view addSubview:_askView];
    [_askView.mainTable setDelegate:self];
}

-(void)viewWillAppear:(BOOL)animated
{
    for (UIView *view in navigationBarView.subviews) {
        [view removeFromSuperview];
    }
    [navigationBarView setTitle:@"最热提问"];
    [self showNavbarStyle];
    
    if (isFirst == NO) {
        isFirst = YES;
        //是否存在本地数据
        NSString *local = [self getLocalData];
        if (local) {
            NSDictionary *dic = [local objectFromJSONString];
            NSArray *newArray = [dic objectForKey:@"list"];
            NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
            for (int i = newArray.count-1 ; i >= 0; i--) {
                [array addObject:[NSMutableDictionary dictionaryWithDictionary:[newArray objectAtIndex:i]]];
            }
            _askView.mainTable.autoDataArray = array;
            self.normalArray = _askView.mainTable.autoDataArray;
        }else{
            [_askView.mainTable autoPullGetData];
        }
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//搜索
-(void)searchText
{
    _isSearchState = YES;
//    _askView.mainTable.autoDataArray = nil;
    [self addData:nil];
}

-(void)pushAskVc
{
    NSArray *array = AppDelegate_A.leftVCArray;
    //营养咨询
    for (UIViewController *vc in array) {
        if ([vc isKindOfClass:[ConsultViewController class]]) {
            ConsultViewController *view = (ConsultViewController*)vc;
            view.showBackBtn = YES;
            [self.navigationController pushViewController:view animated:YES];
            break;
        }
    }
}

-(void)bgButt
{
    [_askView.searchField resignFirstResponder];
}

#pragma mark - AutoRefreshTableViewDelegate
//刷新数据
-(void)refreshTableData
{
    [self addData:nil];
}

//加载更多数据
-(void)loadingMoreTableData
{
    //
    NSDictionary *dic = _askView.mainTable.autoDataArray.lastObject;
    [self addData:[dic valueForKey:@"id"]];
}

-(void)addData:(NSString*)lastId
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setValue:IOS_CONSULTLIST forKey:IOS_METHOD_NAME];
    [dic setValue:[NSNumber numberWithInt:IOS_GET_NUMBER] forKey:@"count"];
//    [dic setValue:[NSNumber numberWithInt:IOS_GET_NUMBER] forKey:@"order"];
    
    BOOL show = NO;
    if (_isSearchState && _askView.searchField.text.length>0) {
        [LoadingFrameView showLoadingview];
        [dic setValue:_askView.searchField.text forKey:@"query"];
        show = YES;
    }else{
        _isSearchState = NO;
    }
    if (lastId) {
        [dic setValue:lastId forKey:@"max_id"];
    }
    
    ASIHTTPRequest *request =  [NetUtls httpGet:self withValue:dic tag:IOS_CONSULTLIST_TAG need:show];
    [self.netArray addObject:request];
    [request startAsynchronous];
}

#pragma mark - ASIHTTPRequestDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@",[request responseString]);
    NSDictionary *dic = [[request responseString] objectFromJSONString];
    if (dic != nil) {
        NSArray *newArray = [dic objectForKey:@"list"];
        if (newArray.count>0) {
            NSMutableArray *array = nil;
            if (_askView.mainTable.currentRefreshState == LOADING_MORE_STATE) {
                array = [NSMutableArray arrayWithArray:_askView.mainTable.autoDataArray];
            }else{
                //存储最新的数据信息
                if (!_isSearchState) {
                    [self saveLocalData:[request responseString]];
                }
                array = [NSMutableArray arrayWithCapacity:1];
            }
            for (int i = newArray.count-1 ; i >= 0; i--) {
                [array addObject:[NSMutableDictionary dictionaryWithDictionary:[newArray objectAtIndex:i]]];
            }
            _askView.mainTable.autoDataArray = array;
            if (!_isSearchState) {
                self.normalArray = _askView.mainTable.autoDataArray;
            }
            [LoadingFrameView directlyDisappear];
        }else{
            if (_askView.mainTable.currentRefreshState == LOADING_MORE_STATE) {
                _askView.mainTable.autoDataArray = _askView.mainTable.autoDataArray;
                if (!_isSearchState) {
                    self.normalArray = _askView.mainTable.autoDataArray;
                }
            }else{
                [_askView.mainTable recoverShowState];
            }
            [LoadingFrameView autoDisappearTipview:@"没有您要搜索的内容"];
        }
    }else{
        [self requestFailed:request];
    }
}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    [_askView.mainTable recoverShowState];
}

#pragma mark - NSNotificationCenter
-(void)keyboardWillShow
{
    if (!bgButton) {
        bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [bgButton setFrame:self.view.bounds];
        [self.view addSubview:bgButton];
        [bgButton setBackgroundColor:[UIColor clearColor]];
        [bgButton addTarget:self action:@selector(bgButt) forControlEvents:UIControlEventTouchUpInside];
    }
    [bgButton setHidden:NO];
}

-(void)keyboardWillHide
{
    [bgButton setHidden:YES];
    if (_isSearchState && _askView.searchField.text.length == 0) {
        _askView.mainTable.autoDataArray = self.normalArray;
        _isSearchState = NO;
    }
}

@end
